﻿using Learun.Util;
using Learun.Util.SqlSugar;

namespace Learun.Application.TwoDevelopment.ZZDT_EC
{
    public class ProjectSugar //不能是泛型类
    {
        /// <summary>
        /// 按项目取表名公用方法
        /// </summary>
        /// <typeparam name="T">实体类Entity</typeparam>
        /// <param name="projectIdOrIndex">项目ID或Index</param>
        /// <returns></returns>
        public static string TableName<T>(string projectIdOrIndex) where T : class, new()
        {
            //按项目取表名公用方法
            string tableName = SqlSugarHelper.Db.EntityMaintenance.GetTableName<T>();
            if (int.TryParse(projectIdOrIndex, out int ProjIndex))
            {
                tableName += "_" + projectIdOrIndex;//免得再去查一下数据库
            }
            else if (!projectIdOrIndex.IsEmpty())
            {
                var project = SqlSugarHelper.Db.Queryable<ec_projectEntity>().First(x => x.ProjectId == projectIdOrIndex);
                if (project != null)
                {
                    tableName += "_" + project.ProjectIndex.Value;
                }
                else
                {
                    throw ExceptionEx.ThrowServiceException(new System.Exception(),"无效的ProjectId");
                }
            }
            return tableName;
        }
    }
}
